var ShareAdd = function(){
	this.init();
}

ShareAdd.prototype={
	init: function(){
		var _this = this;
		// 选择时间和日期
		$(".form-datetime").datetimepicker({
			weekStart: 1,
			todayBtn: 1,
			autoclose: 1,
			todayHighlight: 1,
			startView: 2,
			forceParse: 0,
			showMeridian: 1,
			format: "yyyy-mm-dd hh:ii"
		});
		_this.typeChange();
		
		//推广方案
		new ModalChoseTable({
			modalSize: "400px",
			id: "promotioScheme",
			dataUrl: "http://112.124.121.34:8091/shihou.forum.service/forum/query?activityId=3149",
			dataParameter: {"activityId":"3149"},
			itemNameKey: 'content',
			itemIdKey: 'id'
		});
		
		//新用户注册奖励【添加】
		$('.new-user-reward-add-btn').on("click", function(){
			var index = $('section.new-user-reward-section .form-group').length;
			if($('section.new-user-reward-section .form-group').length > 4)
				return;
			$('section.new-user-reward-section').append(_this.newUserReward(index));
			showSelect($('section.new-user-reward-section .form-group:last'), 'new-user-reward-', 1);
			//渲染select change事件
			$('.new-user-reward-select').on("change", function(){
				var value = $(this).val();
				if(value === "4"){
					newUserRewardScheme(index);
				}
				showSelect($(this).parent().parent(), 'new-user-reward-', value);
			});
			deleteBtn();
		});
		
		function newUserRewardScheme(index){
			new ModalChoseTable({
				modalSize: "400px",
				id: "newUserReward"+index,
				dataUrl: "http://112.124.121.34:8091/shihou.forum.service/forum/query",
				dataParameter: {"activityId":"3149"},
				itemNameKey: 'content',
				itemIdKey: 'id'
			});
		}
		
		//老用户注册奖励【添加】
		$('.old-user-reward-add-btn').on("click", function(){
			var index = $('section.old-user-reward-section .form-group').length;
			if($('section.old-user-reward-section .form-group').length > 4)
				return;
			$('section.old-user-reward-section').append(_this.oldUserReward(index));
			showSelect($('section.old-user-reward-section .form-group:last'), 'old-user-reward-', 1);
			//渲染select change事件
			$('.old-user-reward-select').on("change", function(){
				var value = $(this).val();
				if(value === "4"){
					newUserRewardScheme(index);
				}
				showSelect($(this).parent().parent(), 'old-user-reward-', value);
			});
			deleteBtn();
		});
		
		function oldUserRewardScheme(index){
			new ModalChoseTable({
				modalSize: "400px",
				id: "oldUserReward"+index,
				dataUrl: "http://112.124.121.34:8091/shihou.forum.service/forum/query",
				dataParameter: {"activityId":"3149"},
				itemNameKey: 'content',
				itemIdKey: 'id'
			});
		}
		
		function showSelect(selector, classname, value){
			for(var i = 1;i < 5;i++){
				selector.find('.'+classname+i).hide();
			}
			selector.find('.'+classname+value).show();
		}
		
		function deleteBtn(){
			$('.btn-delete').on("click", function(){
				$(this).parent().parent().remove();
			});
		}
	},
	/**
	 * 点击类型radio进行变换
	 */
	typeChange: function(){
		var type = ['room-activity', 'overall-room-activity', 'ad-share-activity', 'template-activity'];
		$('.activity-type input[type=radio]').click(function(){
			var value = $(this).val();
			switch(value){
				case "1":
					swichoverShow(type[0]);
					break;
				case "2":
					swichoverShow(type[1]);
					break;
				case "3":
					swichoverShow(type[2]);
					break;
				case "4":
					swichoverShow(type[3]);
					break;
				default:
					break;
			}
		});
		
		function swichoverShow(show){
			//先隐藏，再显示。这样的做法时因为房间活动和全局房间活动公用【弹窗图片】
			type.forEach(function(value){
				$("."+value).hide();
			});
			$("."+show).show();
		}
		
		//初始化时，触发点击【房间活动】radio
		$('.activity-type input[type=radio]').eq(0).click();
	},
	newUserReward: function(index){
		var html = '<div class="form-group">'+
					'	<label class="col-xs-2"></label>'+
					'	<div class="col-xs-1">'+
					'		<select class="form-control new-user-reward-select">'+
					'			<option value="1" selected="">狮毛</option>'+
					'			<option value="2">狮牙</option>'+
					'			<option value="3">VIP特权</option>'+
					'			<option value="4">抽奖</option>'+
					'		</select>'+
					'	</div>'+
					'	<label class="col-xs-1">'+
					'		<p class="text-center new-user-reward-1 new-user-reward-2">金额</p>'+
					'		<p class="text-center new-user-reward-3">天数</p>'+
					'		<p class="text-center new-user-reward-4">次数</p>'+
					'	</label>'+
					'	<div class="col-xs-1">'+
					'		<input class="form-control" type="number" min="0" />'+
					'	</div>'+
					'	<label class="col-xs-1">'+
					'		<p class="text-center new-user-reward-3">类型</p>'+
					'		<p class="text-center new-user-reward-4">方案</p>'+
					'	</label>'+
					'	<div class="col-xs-1">'+
					'		<input type="text" class="form-control modal-chose-solution new-user-reward-4" id="newUserReward'+index+'" readonly="readonly" placeholder="点击选择方案" value="" />'+
					'		<select class="form-control new-user-reward-3">'+
					'			<option selected="">白银</option>'+
					'			<option>黄金</option>'+
					'			<option>铂金</option>'+
					'		</select>'+
					'	</div>'+
					'	<div class="col-xs-1">'+
					'		<input type="button" class="btn btn-danger btn-block btn-delete" value="删除"></input>'+
					'	</div>'+
					'	<div class="col-xs-1">'+
					'		<button class="btn btn-primary btn-block">保存</button>'+
					'	</div>'+
					'</div>';
		return html;
	},
	oldUserReward: function(index){
		var html = '<div class="form-group">'+
					'	<label class="col-xs-2"></label>'+
					'	<div class="col-xs-1">'+
					'		<select class="form-control old-user-reward-select">'+
					'			<option value="1" selected="">狮毛</option>'+
					'			<option value="2">狮牙</option>'+
					'			<option value="3">VIP特权</option>'+
					'			<option value="4">抽奖</option>'+
					'		</select>'+
					'	</div>'+
					'	<label class="col-xs-1">'+
					'		<p class="text-center old-user-reward-1 old-user-reward-2">金额</p>'+
					'		<p class="text-center old-user-reward-3">天数</p>'+
					'		<p class="text-center old-user-reward-4">次数</p>'+
					'	</label>'+
					'	<div class="col-xs-1">'+
					'		<input class="form-control" type="number" min="0" />'+
					'	</div>'+
					'	<label class="col-xs-1">'+
					'		<p class="text-center old-user-reward-3">类型</p>'+
					'		<p class="text-center old-user-reward-4">方案</p>'+
					'	</label>'+
					'	<div class="col-xs-1">'+
					'		<input type="text" class="form-control modal-chose-solution old-user-reward-4" id="newUserReward'+index+'" readonly="readonly" placeholder="点击选择方案" value="" />'+
					'		<select class="form-control old-user-reward-3">'+
					'			<option selected="">白银</option>'+
					'			<option>黄金</option>'+
					'			<option>铂金</option>'+
					'		</select>'+
					'	</div>'+
					'<label class="col-xs-1">'+
					'	<p class="text-center">达标次数</p>'+
					'</label>'+
					'<div class="col-xs-1">'+
					'	<input type="number" class="form-control" min="0" />'+
					'</div>'+
					'<div class="col-xs-1">'+
					'	<input type="checkbox" />多次达标'+
					'</div>'+
					'	<div class="col-xs-1">'+
					'		<input type="button" class="btn btn-danger btn-block btn-delete" value="删除"></input>'+
					'	</div>'+
					'	<div class="col-xs-1">'+
					'		<button class="btn btn-primary btn-block">保存</button>'+
					'	</div>'+
					'</div>';
		return html;
	}
}

$(document).ready(function(){
	new ShareAdd();
});
